/**
 * 
 */
package com.yidu.erp.service.ware;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.yidu.common.ReadWareExcel;
import com.yidu.common.SsmMessage;
import com.yidu.common.Tools;
import com.yidu.erp.action.ware.vo.WareVo;
import com.yidu.erp.dao.BeyondDepMapper;
import com.yidu.erp.dao.BeyondWareMapper;
import com.yidu.erp.model.BeyondDep;
import com.yidu.erp.model.BeyondWare;

/**
 * 类的描述：药品Service的实现类
 * @author xym
 *
 * @date 2017年11月29日
 */
@Service
public class WareServiceImpl implements WareService{

	@Resource
	private BeyondWareMapper beyondWareMapper;//药品的mapper
	@Resource
	private BeyondDepMapper beyondDepMapper;//合成表的mapper
 
	/* (non-Javadoc)
	 * @see com.yidu.erp.service.ware.WareService#map(java.util.Map)
	 */
	@Override
	public Map<String, Object> showAll(Map<String, Object> param) {
		//定义一个返回的Map集合
		Map<String, Object> result=new HashMap<String,Object>();
		//得到返回的药品集合
		List<WareVo> warelist=beyondWareMapper.showAll(param);
		//循环将药品集合内的所有值迭代出来并查出配料的集合
		for (Iterator<WareVo> iterator = warelist.iterator(); iterator.hasNext();) {
			WareVo wareVo = (WareVo) iterator.next();
			wareVo.setDepList(beyondDepMapper.findByWareId(wareVo.getWareId()));
		}
		//将药品集合传入map中
		result.put("data", warelist);
		//放入layui显示必须的编码
		result.put("code", 0);
		//放入layui显示必须的消息
		result.put("msg", 0);
		//放入查询出来的总行数
		result.put("count", beyondWareMapper.showCount(param));
		return result;
	}
	/* (non-Javadoc)
	 * @see com.yidu.erp.service.ware.WareService#delete(com.yidu.erp.model.BeyondWare)
	 */
	@Override
	public SsmMessage delete(BeyondWare ware) {
		//新建一个消息类
		SsmMessage mes=new SsmMessage();
		//设置药品是否有效为0
		ware.setIsva("0");
		//调用药品的修改方法得到返回行数
		int row=beyondWareMapper.updateByPrimaryKeySelective(ware);
		//设置消息类的值
		if(row>0){
			mes.setMes(SsmMessage.SUCCESS_MES);
			mes.setState(SsmMessage.SUCCESS);
		}else{
			mes.setMes(SsmMessage.ERROR_MES);
			mes.setState(SsmMessage.ERROR);
		}
		return mes;
	}

	public SsmMessage insert(BeyondWare ware){
		//新建一个消息类
		SsmMessage mes=new SsmMessage();
		//调用根据值增加的方法得到返回行数
		int row=beyondWareMapper.insertSelective(ware);
		//设置消息类的值
		if(row>0){
			mes.setMes(SsmMessage.SUCCESS_MES);
			mes.setState(SsmMessage.SUCCESS);
		}else{
			mes.setMes(SsmMessage.ERROR_MES);
			mes.setState(SsmMessage.ERROR);
		}
		return mes;
	}
	/* (non-Javadoc)
	 * @see com.yidu.erp.service.ware.WareService#findById(java.lang.String)
	 */
	@Override
	public WareVo findByIdVo(String wareId) {
		//调用根据编号查询vo对象的方法得到对象
		WareVo ware=beyondWareMapper.findByIdVo(wareId);
		//调用根据药品编号查询配料的方法得到配料集合
		ware.setDepList(beyondDepMapper.findByWareId(wareId));
		return ware;
	}



	@Override
	public BeyondWare selectByPrimaryKey(String wareId) {
		// TODO Auto-generated method stub
		return beyondWareMapper.selectByPrimaryKey(wareId);
	}
	/* (non-Javadoc)
	 * @see com.yidu.erp.service.ware.WareService#addOrUpdate(com.yidu.erp.model.BeyondWare, java.lang.String, org.springframework.web.multipart.MultipartFile)
	 */
	@Override
	public SsmMessage addOrUpdate(BeyondWare ware, String dep, MultipartFile file) throws NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ParseException {
		//判断是否成功
		boolean result=false;
		//设置药品是否有效属性为1
		ware.setIsva("1");
		// 当图片文件有值时进行图片上传
		if(!"".equals(file.getOriginalFilename())&&null!=file.getOriginalFilename()){
			//得到文件原名
			String fileName=file.getOriginalFilename();
			//得到原始文件的后缀名
			String prefix=fileName.substring(fileName.lastIndexOf("."));
			//利用uuid和后缀名重写
			String pathName=UUID.randomUUID().toString()+prefix;
			//新建路径为'D://Upload//img//'加上文件名
			File path=new File("D://Upload//img//"+pathName);
			try {
				//创建文件
				file.transferTo(path);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//给对象中的图片地址赋值
			ware.setWarePhoto("/upload//"+pathName);
		}
		//当药品的编号为空时进行增加药品的操作。当药品的编号不为空时则进行修改
		if(null!=ware.getWareId()&&!"".equals(ware.getWareId())){
			//调用修改方法判断是否执行行数是否大于0得到布尔值
			result=beyondWareMapper.updateByPrimaryKeySelective(ware)>0;
		}else{
			//调用增加方法判断是否执行行数是否大于0得到布尔值
			result=beyondWareMapper.insertSelective(ware)>0;
		}
		if(null!=dep&&!"".equals(dep)&&result!=false){
			String wareId=ware.getWareId();
			//修改配方的方法
			addDep(dep,wareId);
		}
		//新建一个消息类
		SsmMessage mes=new SsmMessage();
		//设置消息类的内容
		if(result){
			mes.setMes(SsmMessage.SUCCESS_MES);
			mes.setState(SsmMessage.SUCCESS);
		}else{
			mes.setMes(SsmMessage.ERROR_MES);
			mes.setState(SsmMessage.ERROR);
		}
		return mes;
	}
	
	
	/**
	 * 更改商品配料的方法
	 * @param dep 配料的字符串
	 * @param wareId 商品的编号
	 * @return 是否成功
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 * @throws ParseException
	 */
	public SsmMessage addDep(String dep,String wareId) throws NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ParseException{
		SsmMessage mes=new SsmMessage();
		beyondDepMapper.deleteByWareId(wareId);
		//使用"_"分割字符串得到每个对象内值的数组
		String obj[] =dep.split("_");
		//将数组循环迭代
		for (int i = 0; i < obj.length; i++) {
			BeyondDep beyondDep=new BeyondDep();
			//使用","进行分割得到结果如'dosId=1'
			String value[]=obj[i].split(",");
			//迭代出所有的值
			for (int j = 0; j < value.length; j++) {
				//调用将字符串转换成
				Tools.segmentation(beyondDep, value, j);
			}
			beyondDep.setWareId(wareId);
			//调用增加配料的方法给消息对象赋值
			if(beyondDepMapper.insertSelective(beyondDep)>0){
				mes.setMes(SsmMessage.SUCCESS_MES);
				mes.setState(SsmMessage.SUCCESS);
			}else{
				mes.setMes(SsmMessage.ERROR_MES);
				mes.setState(SsmMessage.ERROR);
			}
		}
		return mes;
	}
	/* (non-Javadoc)
	 * @see com.yidu.erp.service.ware.WareService#findByApplyId(java.lang.String)
	 */
	@Override
	public List<BeyondWare> findByApplyId(String applyId) {
		return beyondWareMapper.findByApplyId(applyId);
	}
	
	@Override
	public String readExcelFile(MultipartFile file) {
		String result ="";  
        //创建处理EXCEL的类  
        ReadWareExcel readExcel=new ReadWareExcel();  
        //解析excel，获取上传的事件单  
        List<WareVo> wareList = readExcel.getExcelInfo(file);
        int rows = beyondWareMapper.insertExcel(wareList);  
        //和你具体业务有关,这里不做具体的示范  
        if(rows>0){  
            result = "1";  
        }else{  
            result = "0";  
        }  
        return result; 
	}


}
